package com.example.myapplication2;

import android.annotation.SuppressLint;
import android.os.Environment;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

import java.io.*;

public class FileStorageActivity extends AppCompatActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {

    private EditText input;

    private int k = 1;

    private String fileName = "kk.txt";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_storage);
        input = findViewById(R.id.FileStorageInput);
        findViewById(R.id.FileStorageSave).setOnClickListener(this);
        findViewById(R.id.FileStorageRead).setOnClickListener(this);
        RadioGroup radioGroup = findViewById(R.id.RG2);
        //单选监听和实现
        radioGroup.setOnCheckedChangeListener(this);
    }

    @SuppressLint("SdCardPath")
    @Override
    public void onClick(View view) {
        String dir=null;
        if (k == 1) {
            dir=getFilesDir().toString();
        } else if (k == 2) {
            // /mnt/sdcard/Android/data/****
            dir = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString();
        } else if (k == 3) {
            // sdcard
            dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
            //值为 /storage/emulated/0/Download
        }
        String path=dir+File.separatorChar+fileName;
        String info="";
        switch (view.getId()) {
            case R.id.FileStorageSave:
                Editable text = input.getText();
                if (text != null) {
                    info = text.toString();
                    FileSave(info,path);
                }
                break;
            case R.id.FileStorageRead:
                info = FileRead(path);
                input.setText(info);
                break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i) {
            case R.id.rb_M2:
                // data/data
                k = 1;
                break;
            case R.id.rb_G2:
                // Android/data
                k = 2;
                break;
            case R.id.rb_K2:
                // sdcard空间
                k = 3;
                break;
        }
    }

    //文件读取
    String FileRead(String path){
        BufferedReader bufferedReader=null;
        StringBuilder info= new StringBuilder("");
        try {
            bufferedReader=new BufferedReader(new FileReader(path));
            String k="";
            while (null != (k = bufferedReader.readLine())){
                info.append(k);
            }
        }catch (Exception e){

        }finally {
            if (bufferedReader!=null){
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return info.toString();
    }

    //文件写入
    void FileSave(String info, String path){
        BufferedWriter bufferedWriter=null;
        try {
            bufferedWriter=new BufferedWriter(new FileWriter(path));
            bufferedWriter.write(info);
        }catch (Exception e){
        }finally {
            if (bufferedWriter!=null){
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

}